/**
 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.vijayan.profile.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import com.vijayan.profile.NoSuchFamilyMemberException;
import com.vijayan.profile.model.FamilyMember;
import com.vijayan.profile.model.impl.FamilyMemberImpl;
import com.vijayan.profile.model.impl.FamilyMemberModelImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the family member service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link FamilyMemberUtil} to access the family member persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author vijayan
 * @see FamilyMemberPersistence
 * @see FamilyMemberUtil
 * @generated
 */
public class FamilyMemberPersistenceImpl extends BasePersistenceImpl<FamilyMember>
	implements FamilyMemberPersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = FamilyMemberImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(FamilyMemberModelImpl.ENTITY_CACHE_ENABLED,
			FamilyMemberModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(FamilyMemberModelImpl.ENTITY_CACHE_ENABLED,
			FamilyMemberModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countAll", new String[0]);

	/**
	 * Caches the family member in the entity cache if it is enabled.
	 *
	 * @param familyMember the family member to cache
	 */
	public void cacheResult(FamilyMember familyMember) {
		EntityCacheUtil.putResult(FamilyMemberModelImpl.ENTITY_CACHE_ENABLED,
			FamilyMemberImpl.class, familyMember.getPrimaryKey(), familyMember);
	}

	/**
	 * Caches the family members in the entity cache if it is enabled.
	 *
	 * @param familyMembers the family members to cache
	 */
	public void cacheResult(List<FamilyMember> familyMembers) {
		for (FamilyMember familyMember : familyMembers) {
			if (EntityCacheUtil.getResult(
						FamilyMemberModelImpl.ENTITY_CACHE_ENABLED,
						FamilyMemberImpl.class, familyMember.getPrimaryKey(),
						this) == null) {
				cacheResult(familyMember);
			}
		}
	}

	/**
	 * Clears the cache for all family members.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(FamilyMemberImpl.class.getName());
		EntityCacheUtil.clearCache(FamilyMemberImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the family member.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(FamilyMember familyMember) {
		EntityCacheUtil.removeResult(FamilyMemberModelImpl.ENTITY_CACHE_ENABLED,
			FamilyMemberImpl.class, familyMember.getPrimaryKey());
	}

	/**
	 * Creates a new family member with the primary key. Does not add the family member to the database.
	 *
	 * @param memberId the primary key for the new family member
	 * @return the new family member
	 */
	public FamilyMember create(long memberId) {
		FamilyMember familyMember = new FamilyMemberImpl();

		familyMember.setNew(true);
		familyMember.setPrimaryKey(memberId);

		return familyMember;
	}

	/**
	 * Removes the family member with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the family member to remove
	 * @return the family member that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a family member with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public FamilyMember remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove(((Long)primaryKey).longValue());
	}

	/**
	 * Removes the family member with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param memberId the primary key of the family member to remove
	 * @return the family member that was removed
	 * @throws com.vijayan.profile.NoSuchFamilyMemberException if a family member with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public FamilyMember remove(long memberId)
		throws NoSuchFamilyMemberException, SystemException {
		Session session = null;

		try {
			session = openSession();

			FamilyMember familyMember = (FamilyMember)session.get(FamilyMemberImpl.class,
					new Long(memberId));

			if (familyMember == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + memberId);
				}

				throw new NoSuchFamilyMemberException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					memberId);
			}

			return remove(familyMember);
		}
		catch (NoSuchFamilyMemberException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected FamilyMember removeImpl(FamilyMember familyMember)
		throws SystemException {
		familyMember = toUnwrappedModel(familyMember);

		Session session = null;

		try {
			session = openSession();

			if (familyMember.isCachedModel() || BatchSessionUtil.isEnabled()) {
				Object staleObject = session.get(FamilyMemberImpl.class,
						familyMember.getPrimaryKeyObj());

				if (staleObject != null) {
					session.evict(staleObject);
				}
			}

			session.delete(familyMember);

			session.flush();
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.removeResult(FamilyMemberModelImpl.ENTITY_CACHE_ENABLED,
			FamilyMemberImpl.class, familyMember.getPrimaryKey());

		return familyMember;
	}

	public FamilyMember updateImpl(
		com.vijayan.profile.model.FamilyMember familyMember, boolean merge)
		throws SystemException {
		familyMember = toUnwrappedModel(familyMember);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, familyMember, merge);

			familyMember.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(FamilyMemberModelImpl.ENTITY_CACHE_ENABLED,
			FamilyMemberImpl.class, familyMember.getPrimaryKey(), familyMember);

		return familyMember;
	}

	protected FamilyMember toUnwrappedModel(FamilyMember familyMember) {
		if (familyMember instanceof FamilyMemberImpl) {
			return familyMember;
		}

		FamilyMemberImpl familyMemberImpl = new FamilyMemberImpl();

		familyMemberImpl.setNew(familyMember.isNew());
		familyMemberImpl.setPrimaryKey(familyMember.getPrimaryKey());

		familyMemberImpl.setMemberId(familyMember.getMemberId());
		familyMemberImpl.setProfileId(familyMember.getProfileId());
		familyMemberImpl.setMemberName(familyMember.getMemberName());
		familyMemberImpl.setRelationship(familyMember.getRelationship());
		familyMemberImpl.setMarried(familyMember.isMarried());
		familyMemberImpl.setAvailable(familyMember.isAvailable());

		return familyMemberImpl;
	}

	/**
	 * Finds the family member with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the family member to find
	 * @return the family member
	 * @throws com.liferay.portal.NoSuchModelException if a family member with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public FamilyMember findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the family member with the primary key or throws a {@link com.vijayan.profile.NoSuchFamilyMemberException} if it could not be found.
	 *
	 * @param memberId the primary key of the family member to find
	 * @return the family member
	 * @throws com.vijayan.profile.NoSuchFamilyMemberException if a family member with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public FamilyMember findByPrimaryKey(long memberId)
		throws NoSuchFamilyMemberException, SystemException {
		FamilyMember familyMember = fetchByPrimaryKey(memberId);

		if (familyMember == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + memberId);
			}

			throw new NoSuchFamilyMemberException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				memberId);
		}

		return familyMember;
	}

	/**
	 * Finds the family member with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the family member to find
	 * @return the family member, or <code>null</code> if a family member with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public FamilyMember fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the family member with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param memberId the primary key of the family member to find
	 * @return the family member, or <code>null</code> if a family member with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public FamilyMember fetchByPrimaryKey(long memberId)
		throws SystemException {
		FamilyMember familyMember = (FamilyMember)EntityCacheUtil.getResult(FamilyMemberModelImpl.ENTITY_CACHE_ENABLED,
				FamilyMemberImpl.class, memberId, this);

		if (familyMember == null) {
			Session session = null;

			try {
				session = openSession();

				familyMember = (FamilyMember)session.get(FamilyMemberImpl.class,
						new Long(memberId));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (familyMember != null) {
					cacheResult(familyMember);
				}

				closeSession(session);
			}
		}

		return familyMember;
	}

	/**
	 * Finds all the family members.
	 *
	 * @return the family members
	 * @throws SystemException if a system exception occurred
	 */
	public List<FamilyMember> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the family members.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of family members to return
	 * @param end the upper bound of the range of family members to return (not inclusive)
	 * @return the range of family members
	 * @throws SystemException if a system exception occurred
	 */
	public List<FamilyMember> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the family members.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of family members to return
	 * @param end the upper bound of the range of family members to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of family members
	 * @throws SystemException if a system exception occurred
	 */
	public List<FamilyMember> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<FamilyMember> list = (List<FamilyMember>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			Session session = null;

			try {
				session = openSession();

				StringBundler query = null;
				String sql = null;

				if (orderByComparator != null) {
					query = new StringBundler(2 +
							(orderByComparator.getOrderByFields().length * 3));

					query.append(_SQL_SELECT_FAMILYMEMBER);

					appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
						orderByComparator);

					sql = query.toString();
				}
				else {
					sql = _SQL_SELECT_FAMILYMEMBER;
				}

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<FamilyMember>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<FamilyMember>)QueryUtil.list(q, getDialect(),
							start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					list = new ArrayList<FamilyMember>();
				}

				cacheResult(list);

				FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the family members from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (FamilyMember familyMember : findAll()) {
			remove(familyMember);
		}
	}

	/**
	 * Counts all the family members.
	 *
	 * @return the number of family members
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_FAMILYMEMBER);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the family member persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.vijayan.profile.model.FamilyMember")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<FamilyMember>> listenersList = new ArrayList<ModelListener<FamilyMember>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<FamilyMember>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	@BeanReference(type = ProfilePersistence.class)
	protected ProfilePersistence profilePersistence;
	@BeanReference(type = MyExpectationPersistence.class)
	protected MyExpectationPersistence myExpectationPersistence;
	@BeanReference(type = FamilyMemberPersistence.class)
	protected FamilyMemberPersistence familyMemberPersistence;
	@BeanReference(type = HoroscopePersistence.class)
	protected HoroscopePersistence horoscopePersistence;
	@BeanReference(type = MyPhotoPersistence.class)
	protected MyPhotoPersistence myPhotoPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_FAMILYMEMBER = "SELECT familyMember FROM FamilyMember familyMember";
	private static final String _SQL_COUNT_FAMILYMEMBER = "SELECT COUNT(familyMember) FROM FamilyMember familyMember";
	private static final String _ORDER_BY_ENTITY_ALIAS = "familyMember.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No FamilyMember exists with the primary key ";
	private static Log _log = LogFactoryUtil.getLog(FamilyMemberPersistenceImpl.class);
}